/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.date.DateUtil
 *  cn.hutool.core.util.ObjectUtil
 *  com.alibaba.fastjson.JSON
 *  com.sun.jna.Library
 *  com.sun.jna.Native
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.stereotype.Service
 */
package com.gys.common.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.gys.common.business.SaleBillSetUtil;
import com.gys.common.business.SalesReceiptsUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.util.CacheUtil;
import com.gys.controller.BaseController;
import com.gys.controller.salesReceipts.SalesReceiptsMainController;
import com.gys.data.example.GetUserOutData;
import com.gys.data.saleBillSet.GaiaSdPrintSale;
import com.gys.data.salesReceipts.GetQueryMemberOutData;
import com.gys.data.salesReceipts.GetRestOrderDetailOutData;
import com.gys.data.salesReceipts.GetSalesReceiptsTableOutData;
import com.sun.jna.Library;
import com.sun.jna.Native;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import javafx.scene.control.Alert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PrintService
extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(PrintService.class);
    @Autowired
    private SalesReceiptsMainController salesReceiptsMainController;
    static TerMbDll dll = (TerMbDll)PrintService.init(TerMbDll.class);
    private static String width = " ";

    private static <T> T init(Class c) {
        String dllPath = "/JsPrinterDll_64.dll";
        if (!Library.class.isAssignableFrom(c)) {
            return null;
        }
        Library load = Native.load((String)dllPath, (Class)c);
        return (T)load;
    }

    public void printTest() {
        HttpJson prHttpJson = SaleBillSetUtil.get();
        GaiaSdPrintSale printSale = null;
        if (prHttpJson.isSuccess()) {
            printSale = (GaiaSdPrintSale)JSON.parseObject((String)prHttpJson.getMsg(), GaiaSdPrintSale.class);
            if (ObjectUtil.isEmpty(printSale)) {
                Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u5148\u8bbe\u7f6e\u9500\u552e\u5c0f\u7968\u6a21\u677f");
                alert.showAndWait();
                return;
            }
        } else {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", prHttpJson.getMsg());
            alert.showAndWait();
            return;
        }
        int fs = dll.uniOpenUsb();
        log.info("fs\u53e5\u67c4:" + fs);
        if (fs <= 0) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u6253\u5370\u673a\u672a\u8fde\u63a5\uff0c\u8bf7\u68c0\u67e5");
            alert.showAndWait();
            return;
        }
        dll.uniinitializePrinter(fs);
        dll.unisetLineSpaceing(fs, 100);
        dll.uniselectOrCancelBoldModel(fs, 11);
        this.plan1Test(fs, printSale);
        dll.uniselectCutPagerModerAndCutPager2(fs, 66, 20);
        dll.uniClose(fs);
    }

    public boolean printSale(String caseId) {
        HashMap<String, Object> param = new HashMap<String, Object>();
        GetUserOutData loginUser = this.getLoginUser();
        param.put("clientId", loginUser.getClient());
        param.put("gssdBillNo", caseId);
        param.put("gssdBrId", loginUser.getDepId());
        HttpJson httpJson = SalesReceiptsUtil.getRestOrderDetail(param);
        List outDataList = null;
        if (!httpJson.isSuccess()) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", httpJson.getMsg());
            alert.showAndWait();
            return false;
        }
        outDataList = JSON.parseArray((String)httpJson.getMsg(), GetRestOrderDetailOutData.class);
        HttpJson prHttpJson = SaleBillSetUtil.get();
        GaiaSdPrintSale printSale = null;
        if (prHttpJson.isSuccess()) {
            printSale = (GaiaSdPrintSale)JSON.parseObject((String)prHttpJson.getMsg(), GaiaSdPrintSale.class);
            if (ObjectUtil.isEmpty(printSale)) {
                Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u5148\u8bbe\u7f6e\u9500\u552e\u5c0f\u7968\u6a21\u677f");
                alert.showAndWait();
                return false;
            }
        } else {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", prHttpJson.getMsg());
            alert.showAndWait();
            return false;
        }
        int fs = dll.uniOpenUsb();
        log.info("fs\u53e5\u67c4:" + fs);
        if (fs <= 0) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u6253\u5370\u673a\u672a\u8fde\u63a5\uff0c\u8bf7\u68c0\u67e5");
            alert.showAndWait();
            return false;
        }
        dll.uniinitializePrinter(fs);
        dll.unisetLineSpaceing(fs, 100);
        dll.uniselectOrCancelBoldModel(fs, 11);
        this.plan1(fs, printSale, caseId, outDataList);
        dll.uniselectCutPagerModerAndCutPager2(fs, 66, 20);
        dll.uniClose(fs);
        return true;
    }

    private void plan1Test(int fs, GaiaSdPrintSale printSale) {
        String title = printSale.getGspsModule1();
        PrintService.uniWrite(fs, title);
        String time = "\u6253\u5370\u65f6\u95f4\uff1a" + DateUtil.now();
        PrintService.uniWrite(fs, time);
        String emp = "\u6536\u94f6\u5458\uff1a";
        PrintService.uniWrite(fs, emp);
        String caseIdStr = "\u5355\u53f7\uff1a";
        PrintService.uniWrite(fs, caseIdStr);
        PrintService.wirteDoubleLine(fs);
        String proTitle = "\u8d27\u53f7  \u54c1\u540d  \u89c4\u683c  \u5382\u5bb6  \u6548\u671f  \u8425\u4e1a\u5458  \u539f\u4ef7  \u5b9e\u4ef7  \u6570\u91cf  \u91d1\u989d";
        PrintService.uniWrite(fs, proTitle);
        PrintService.wirteSleLine(fs);
        String bottom = printSale.getGspsModule6();
        PrintService.uniWrite(fs, bottom);
    }

    private void plan1(int fs, GaiaSdPrintSale printSale, String caseId, List<GetRestOrderDetailOutData> orderDetailList) {
        String title = printSale.getGspsModule1();
        PrintService.uniWrite(fs, title);
        String time = "\u6253\u5370\u65f6\u95f4\uff1a" + DateUtil.now();
        PrintService.uniWrite(fs, time);
        String emp = "\u6536\u94f6\u5458\uff1a" + this.salesReceiptsMainController.getWorkUserId().getText();
        PrintService.uniWrite(fs, emp);
        String caseIdStr = "\u5355\u53f7\uff1a" + caseId;
        PrintService.uniWrite(fs, caseIdStr);
        GetQueryMemberOutData memberOutData = (GetQueryMemberOutData)CacheUtil.get("memberOutData");
        if (ObjectUtil.isNotEmpty(memberOutData)) {
            String cardNo = memberOutData.getCardNum();
            String name = memberOutData.getName();
            String memberInfo = "\u5361\u53f7\uff1a" + cardNo + "  \u59d3\u540d\uff1a" + name;
            PrintService.uniWrite(fs, memberInfo);
            String point = memberOutData.getPoint();
            String nowPoint = memberOutData.getNowPoint();
            String pointStr = "\u4e0a\u6b21\u79ef\u5206\uff1a" + point;
            if (ObjectUtil.isNotEmpty(nowPoint)) {
                pointStr = pointStr + "  \u672c\u6b21\u79ef\u5206\uff1a" + nowPoint + "  \u603b\u79ef\u5206\uff1a" + (Integer.valueOf(point) + Integer.valueOf(nowPoint));
            }
            PrintService.uniWrite(fs, pointStr);
        }
        PrintService.wirteDoubleLine(fs);
        String proTitle = "\u8d27\u53f7  \u54c1\u540d  \u89c4\u683c  \u5382\u5bb6  \u6548\u671f  \u8425\u4e1a\u5458  \u539f\u4ef7  \u5b9e\u4ef7  \u6570\u91cf  \u91d1\u989d";
        PrintService.uniWrite(fs, proTitle);
        PrintService.wirteSleLine(fs);
        for (int i = 0; i < orderDetailList.size(); ++i) {
            GetRestOrderDetailOutData item = orderDetailList.get(i);
            GetSalesReceiptsTableOutData itemPro = orderDetailList.get(i).getGetSalesReceiptsTableOutData();
            String proInfo = i + 1 + "." + item.getGssdBatchNo() + width + item.getGssdProName() + width + item.getSpec() + width + item.getProFactoryName() + width + item.getGssdValidDate() + width + itemPro.getEmpName() + width + itemPro.getPrcAmount() + width + itemPro.getPrice() + width + itemPro.getNum() + width + itemPro.getTotal();
            PrintService.uniWrite(fs, proInfo);
        }
        PrintService.wirteSleLine(fs);
        String bottom = printSale.getGspsModule6();
        PrintService.uniWrite(fs, bottom);
    }

    private static void uniWrite(int fs, String str) {
        byte[] timeByte = PrintService.getBytes(str);
        dll.uniWrite(fs, timeByte, timeByte.length);
        dll.uniprintAndFeedLine(fs);
    }

    private static void wirteDoubleLine(int fs) {
        String str = "==============================";
        byte[] timeByte = PrintService.getBytes(str);
        dll.uniWrite(fs, timeByte, timeByte.length);
        dll.uniprintAndFeedLine(fs);
    }

    private static void wirteSleLine(int fs) {
        String str = "------------------------------";
        byte[] timeByte = PrintService.getBytes(str);
        dll.uniWrite(fs, timeByte, timeByte.length);
        dll.uniprintAndFeedLine(fs);
    }

    private static byte[] getBytes(String str) {
        byte[] bytes = null;
        try {
            bytes = str.getBytes("gbk");
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return bytes;
    }

    public SalesReceiptsMainController getSalesReceiptsMainController() {
        return this.salesReceiptsMainController;
    }

    public void setSalesReceiptsMainController(SalesReceiptsMainController salesReceiptsMainController) {
        this.salesReceiptsMainController = salesReceiptsMainController;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof PrintService)) {
            return false;
        }
        PrintService other = (PrintService)o;
        if (!other.canEqual(this)) {
            return false;
        }
        SalesReceiptsMainController this$salesReceiptsMainController = this.getSalesReceiptsMainController();
        SalesReceiptsMainController other$salesReceiptsMainController = other.getSalesReceiptsMainController();
        return !(this$salesReceiptsMainController == null ? other$salesReceiptsMainController != null : !(this$salesReceiptsMainController).equals(other$salesReceiptsMainController));
    }

    protected boolean canEqual(Object other) {
        return other instanceof PrintService;
    }

    public int hashCode() {
        int PRIME = 59;
        int result = 1;
        SalesReceiptsMainController $salesReceiptsMainController = this.getSalesReceiptsMainController();
        result = result * 59 + ($salesReceiptsMainController == null ? 43 : ($salesReceiptsMainController).hashCode());
        return result;
    }

    public String toString() {
        return "PrintService(salesReceiptsMainController=" + this.getSalesReceiptsMainController() + ")";
    }

    public static interface TerMbDll
    extends Library {
        public int uniOpenUsb();

        public boolean uniClose(int var1);

        public int uniselectAlignment(int var1, int var2);

        public int unisetLeftSpace(int var1, int var2, int var3);

        public int uniselectCharacterSize(int var1, int var2);

        public int uniprintAndFeedLine(int var1);

        public int uniWrite(int var1, byte[] var2, int var3);

        public int uniinitializePrinter(int var1);

        public int unisetChineseCharacterModel(int var1, int var2);

        public int uniselectOrCancelBoldModel(int var1, int var2);

        public int uniselectCutPagerModerAndCutPager2(int var1, int var2, int var3);

        public int uniselectChineseCharModel(int var1);

        public int uniselectCutPagerModerAndCutPager(int var1, int var2);

        public int unisetHorizontalAndVerticalMoveUnit(int var1, int var2, int var3);

        public int unisetDefultLineSpacing(int var1);

        public int unisetLineSpaceing(int var1, int var2);

        public int unisetHorizontalmovementPosition(int var1, char[] var2, int var3);
    }
}

